package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.model.entity.ApartmentInfo;
import com.atguigu.lease.model.entity.FacilityInfo;
import com.atguigu.lease.model.entity.GraphInfo;
import com.atguigu.lease.model.entity.LabelInfo;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.ApartmentInfoService;
import com.atguigu.lease.web.app.service.GraphInfoService;
import com.atguigu.lease.web.app.service.LabelInfoService;
import com.atguigu.lease.web.app.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    @Autowired
    GraphInfoService graphInfoService;
    @Autowired
    LabelInfoService labelInfoService;
    @Autowired
    RoomInfoMapper roomInfoMapper;
    @Autowired
    FacilityInfoMapper facilityInfoMapper;
    @Override
    public ApartmentItemVo getApartmentInfoById(Long apartmentId) {
        ApartmentItemVo apartmentItemVo=new ApartmentItemVo();
            ApartmentInfo apartmentInfo = getById(apartmentId);
            List<GraphInfo> graphVoList= graphInfoService.getGraphInfo(apartmentId,ItemType.APARTMENT);
            List<LabelInfo> labelInfoList= labelInfoService.getLabelInfo(apartmentId);
            BigDecimal minRent = roomInfoMapper.getgetMinRent(apartmentId);
            BeanUtils.copyProperties(apartmentInfo,apartmentItemVo);
            apartmentItemVo.setLabelInfoList(labelInfoList);
            apartmentItemVo.setMinRent(minRent);
            apartmentItemVo.setGraphVoList(graphVoList);


        return apartmentItemVo;
    }

    @Override
    public ApartmentDetailVo getApartmentDetailById(Long id) {
        //1.查询ApartmentInfo
        ApartmentItemVo apartmentInfoById = this.getApartmentInfoById(id);
            if (apartmentInfoById==null){
                return null;
            }
        //2.查询GraphInfo
      //List<GraphInfo> graphInfoList =  graphInfoService.getGraphInfo(id,ItemType.APARTMENT);
        List<GraphVo> graphVoList = graphInfoService.selectListByItemTypeAndId(ItemType.APARTMENT, id);
            //3.查询labelInfoList
        List<LabelInfo> labelInfoLists = labelInfoService.getLabelInfoByRoomId(id);
        //4.查询facilityInfoList
            List<FacilityInfo> facilityInfoList=  facilityInfoMapper.selectListByApartmentId(id);
        //5.查询公寓最低房租
        BigDecimal bigDecimal = roomInfoMapper.getgetMinRent(id);

        ApartmentDetailVo appApartmentDetailVo = new ApartmentDetailVo();
        BeanUtils.copyProperties(apartmentInfoById,appApartmentDetailVo);
        appApartmentDetailVo.setIsDelete(apartmentInfoById.getIsDeleted()==1);
        appApartmentDetailVo.setGraphVoList(graphVoList);
        appApartmentDetailVo.setFacilityInfoList(facilityInfoList);
        appApartmentDetailVo.setMinRent(bigDecimal);
        appApartmentDetailVo.setLabelInfoList(labelInfoLists);
        return appApartmentDetailVo;
    }
}




